package com.lncd.backend.service.impl;

import com.lncd.backend.dto.LocationDTO;
import com.lncd.backend.model.Location;
import com.lncd.backend.model.User;
import com.lncd.backend.repository.LocationRepository;
import com.lncd.backend.repository.UserRepository;
import com.lncd.backend.service.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LocationServiceImpl implements LocationService {

    private final LocationRepository locationRepository;
    private final UserRepository userRepository;

    @Autowired
    public LocationServiceImpl(LocationRepository locationRepository, UserRepository userRepository) {
        this.locationRepository = locationRepository;
        this.userRepository = userRepository;
    }

    @Override
    @Transactional
    public LocationDTO addLocation(LocationDTO locationDTO) {
        User user = userRepository.findById(locationDTO.getUserId())
                .orElseThrow(() -> new RuntimeException("用户不存在"));

        Location location = new Location();
        location.setUser(user);
        location.setLatitude(locationDTO.getLatitude());
        location.setLongitude(locationDTO.getLongitude());
        location.setAddress(locationDTO.getAddress());
        location.setTimestamp(locationDTO.getTimestamp() != null ? locationDTO.getTimestamp() : new Date());

        location = locationRepository.save(location);
        return LocationDTO.fromEntity(location);
    }

    @Override
    public LocationDTO getLocationById(Long id) {
        Location location = locationRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("位置信息不存在"));
        return LocationDTO.fromEntity(location);
    }

    @Override
    public List<LocationDTO> getLocationsByUserId(Long userId) {
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new RuntimeException("用户不存在"));
        
        return locationRepository.findByUserOrderByTimestampDesc(user).stream()
                .map(LocationDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public List<LocationDTO> getLocationsByUserIdAndTimeRange(Long userId, Date startTime, Date endTime) {
        return locationRepository.findByUserIdAndTimestampBetween(userId, startTime, endTime).stream()
                .map(LocationDTO::fromEntity)
                .collect(Collectors.toList());
    }

    @Override
    public LocationDTO getLatestLocationByUserId(Long userId) {
        return locationRepository.findLatestByUserId(userId)
                .map(LocationDTO::fromEntity)
                .orElseThrow(() -> new RuntimeException("没有位置信息"));
    }

    @Override
    @Transactional
    public void deleteLocation(Long id) {
        if (!locationRepository.existsById(id)) {
            throw new RuntimeException("位置信息不存在");
        }
        locationRepository.deleteById(id);
    }
} 